<?php

class M_hoa extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    // lấy dữ liệu theo từng phần 
    function list_all($number, $offset) {

        $query = $this->db->get('hoa', $number, $offset);
        return $query->result_array();
    }

    function list_all_desc($number, $offset) {
        //   $this->db->get('hoa', $number, $offset);
        $this->db->from('hoa');
        $this->db->limit($number, $offset);
        $this->db->order_by("MaHoa", "desc");
        $query = $this->db->get();
        return $query->result_array();
    }

    // đếm tổng số record trong table book 
    function count_all() {
        return $this->db->count_all('hoa');
    }

    function getSoLuong($MaHoa) {
        $results = $this->db->get_where('hoa', array('MaHoa' => $MaHoa))->result();
        $result = $results[0];
        return $result->SoLuong;
    }

    function getProduct($MaHoa) {
        $results = $this->db->get_where('hoa', array('MaHoa' => $MaHoa))->result();
        $result = $results[0];
        return $result;
    }

    function deleteProduct($MaHoa) {
        $this->db->delete('hoa', array('MaHoa' => $MaHoa));
    }

    function insertProduct($data) {
        if ($this->db->insert('hoa', $data)) {
            return true;
        } else {
            return false;
        }
    }

    function updateProduct($MaHoa, $data) {
        $this->db->where('MaHoa', $MaHoa);
        $this->db->update('hoa', $data);
    }
    function updateSoLuong($MaHoa, $SoLuong) {
        if ($this->db->query('UPDATE hoa SET SoLuong = "'.$SoLuong.'" WHERE MaHoa = "' . $MaHoa . '"')) {
            return true;
        } else {
            return false;
        }
    }

    function getLastProduct() {
        $results = $this->db->query('SELECT MaHoa FROM hoa ORDER BY MaHoa DESC LIMIT 1')->result();
        if ($results == null) {
            return 0;
        } else {
            $result = $results[0]->MaHoa;
            return $result;
        }
        //return $x;
    }

}

?>